package springtest.pdf.wk;

import java.io.IOException;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;

/**
 * @author chenzhiqiang
 * @date 2018-08-07 15:40:24
 */

public class ApacheExec {
    private static final String toPdfTool = "D:\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";

    public static void main(String[] args) throws ExecuteException, IOException {
        // a();
        CommandLine commandLine = new CommandLine(toPdfTool);
        commandLine.addArgument("--header-line");// 页眉下面的线
        // 页眉中间内容 命令和参数要分开否则会报错
        commandLine.addArgument(" --header-center").addArgument(" 这里是页眉这里是页眉这里是页眉这里是页眉 ");
        // 设置页面上边距(default 10mm)
        // commandLine.addArgument("--margin-top ").addArgument("30mm");
        commandLine.addArgument("--header-spacing ").addArgument("5");// (设置页眉和内容的距离,默认0)
        commandLine.addArgument("--footer-center").addArgument(" (设置在中心位置的页脚内容)");// 设置在中心位置的页脚内容
        commandLine.addArgument("--footer-line");// * 显示一条线在页脚内容上)
        commandLine.addArgument("--footer-spacing ").addArgument("5");// (设置页脚和内容的距离)
        commandLine.addArgument("--margin-left").addArgument("5mm");
        commandLine.addArgument("--margin-right").addArgument("5mm");
        commandLine.addArgument("--margin-top").addArgument("5mm");
        commandLine.addArgument("--margin-bottom").addArgument("5mm");
        commandLine.addArgument("--disable-external-links");// 禁用网页链接
        commandLine.addArgument("E:/00m2/test/test1.html");// html文件位置
        commandLine.addArgument("d:/wkhtmltopdf.pdf");
        DefaultExecutor executor = new DefaultExecutor();
        int exitValue = executor.execute(commandLine);// 执行
    }

    public static void a() throws ExecuteException, IOException {
        CommandLine commandLine = new CommandLine(toPdfTool);
        commandLine.addArgument("--header-line");// 页眉下面的线
        commandLine.addArgument("--lowquality");
        commandLine.addArgument("--disable-local-file-access");
        commandLine.addArgument("--orientation").addArgument("Landscape");
        commandLine.addArgument("--page-size").addArgument("Letter");
        commandLine.addArgument("--quiet");
        commandLine.addArgument("--margin-left").addArgument("5mm");
        commandLine.addArgument("--margin-right").addArgument("5mm");
        commandLine.addArgument("--margin-top").addArgument("5mm");
        commandLine.addArgument("--margin-bottom").addArgument("5mm");
        commandLine.addArgument("E:/00m2/test/test1.html");// html文件位置
        commandLine.addArgument("d:/wkhtmltopdf.pdf");
        DefaultExecutor executor = new DefaultExecutor();
        int exitValue = executor.execute(commandLine);// 执行
    }
}
